class Wiki::PageController < ApplicationController
    before_filter :authorize, :only => [:edit, :delete]


    def index
      redirect_to :action => "view", :id => 1
    end

    # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
    #verify :method => :post, :only => [ :destroy, :create, :update ],
    #       :redirect_to => { :action => :view, :id => 1 }

    def view
      begin
        @psomething = Page.find(params[:id])
      rescue ActiveRecord::RecordNotFound
        logger.error("Page not found with id #{params[:id]}")
        flash[:notice] = 'invalid page'
        redirect_to :action => "index"
      else
        if(@psomething.user_id == 0 || @psomething.user_id == @session[:user_id])
          @page = @psomething
        else
          redirect_to :action => "error", :id => "permission_error"
        end
      end
    end

    def new
      @page = Page.new
      @submit_name = "Create"
    end

    def create
      @page = Page.new(params[:page])
      if @page.save
        flash[:notice] = 'Page was successfully created.'
        redirect_to :action => 'view', :id => @page.id
        event_add(@params[:controller], @params[:action], @page.id)
      else
        render :action => 'new'
      end

    end

    def edit
      @submit_name = "Edit"
      @page = Page.find(params[:id])
    end

    def update
      @page = Page.find(params[:id])

      # Check if checkbox is unchecked
      if !@params[:page][:user_id] 
        @params[:page][:user_id] = 0
      end

      # Update
      if @page.update_attributes(params[:page])  
        flash[:notice] = 'Page Successful updated'
        event_add(@params[:controller], @params[:action], @page.id)
        redirect_to :action => 'view', :id => @page.id
      else
        render :action => 'edit'
      end
    end

    def destroy
      Page.find(params[:id]).destroy
      flash[:notice] = "Page Deleted"
      event_add(@params[:controller], @params[:action], params[:id])
      redirect_to :action => 'view', :id => 1

    end

    def login
      render :controller => "user", :action => "login"
    end
end
